import "./index.scss";
import { Button, Card, Form, Input, message } from "antd";
import logo from "@/assets/logo.png";
import forms from "./forms";
import { fetchLogin } from "@/store/modules/user";
import { useDispatch } from "react-redux";
import { useNavigate } from "react-router-dom";

const Login = () => {
  // 点击登录按钮时触发 参数values即是表单输入数据
  const dispatch = useDispatch();
  const navigate = useNavigate();
  const onFinish = async (formValue) => {
    await dispatch(fetchLogin(formValue));
    navigate("/");
    message.success("登录成功");
  };
  return (
    <div className="login">
      <Card className="login-container">
        <img className="login-logo" src={logo} alt="" />
        {/* 登录表单 */}
        <Form validateTrigger={"onBlur"} onFinish={onFinish}>
          {forms.map((item) => (
            <Form.Item key={item.label} name={item.name} rules={item.rules}>
              <Input size="large" placeholder={item.placeholder} />
            </Form.Item>
          ))}
          <Form.Item>
            <Button type="primary" htmlType="submit" size="large" block>
              登录
            </Button>
          </Form.Item>
        </Form>
      </Card>
    </div>
  );
};

export default Login;
